﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.IO;
using System.Linq;
using System.Security.AccessControl;
using System.Web.Mvc;
using GuRenFang.Service.Entity;
using GuRenFang.Service.IBiz;

namespace GuRenFang.Controllers.Management
{
    public class WeddingController : Controller
    {
        private readonly IWeddingCategoryBiz _service;

        public WeddingController(IWeddingCategoryBiz service)
        {
            _service = service;
        }

        [Authorize]
        public ActionResult WeddingCategoryList(int? page = 1)
        {
            ViewBag.Model = "Wedding";

            short size = Convert.ToInt16(ConfigurationManager.AppSettings["AdminpageSize"]);

            int totalRecord = 0;
            IEnumerable<WeddingCategory> categorys = _service.GetCategoryByPages(out totalRecord,
                                                                                 pageIndex: page.GetValueOrDefault(),
                                                                                 pageSize: size, orderFiled: "");

            var item = new PageItem
                           {
                               CurrentPage = page.GetValueOrDefault(),
                               TotalRecords = totalRecord,
                               TotalPages = (totalRecord%size == 0) ? totalRecord/size : totalRecord/size + 1,
                               ControllerName = "Wedding",
                               ActionName = "WeddingCategoryList"
                           };

            var viewModels = new Pager<WeddingCategory> {DataSource = categorys, Item = item};

            return View(viewModels);
        }

        [Authorize]
        [HttpPost]
        public ActionResult WeddingCategoryAdd(FormCollection forms)
        {
            if (forms != null && !string.IsNullOrEmpty(forms["txtCategoryName"]))
            {
                string categoryName = forms["txtCategoryName"].Trim();
                var category = new WeddingCategory
                                   {
                                       Name = categoryName,
                                       ThumbnailPath = "/images/NoPicture.jpg",
                                       CreatedTime = DateTime.Now
                                   };
                _service.Create(category);

                var directoryInfo = new DirectoryInfo(Server.MapPath("~/UpFiles/婚纱/" + categoryName + "/"));
                if (!directoryInfo.Exists)
                {
                    directoryInfo.Create();
                }
                directoryInfo = new DirectoryInfo(Server.MapPath("~/UpFiles/婚纱/" + categoryName + "/Thumbnail/"));
                if (!directoryInfo.Exists)
                {
                    directoryInfo.Create();
                }
            }

            return RedirectToAction("WeddingCategoryList");
        }

        [Authorize]
        public ActionResult DeleteWeddingCategory(int id, string name)
        {
            _service.Delete(id);

            Common.DeleteFolder(Server.MapPath("~/UpFiles/婚纱/" + name + "/"));

            return RedirectToAction("WeddingCategoryList");
        }

        [Authorize]
        public ActionResult ShowWeddingCategory(int id, string name)
        {
            if (id <= 0 || string.IsNullOrEmpty(name))
            {
                return RedirectToAction("WeddingCategoryList");
            }

            ViewBag.Model = "Wedding";
            ViewBag.CategoryId = id;
            WeddingCategory category = _service.GetById(id);
            ViewBag.ThumPath = category.ThumbnailPath;

            string categoryName = Server.UrlDecode(name);
            string filePath = "~/UpFiles/婚纱/" + categoryName + "/";
            ViewBag.Category = categoryName;
            ViewBag.Path = filePath;
            List<string> model = Common.GetImages(Server.MapPath(filePath));

            return View(model);
        }

        [Authorize]
        public ActionResult DeleteWeddingPicture(string name, string category, int categoryId)
        {
            ViewBag.Model = "Wedding";
            string xPath = "~/UpFiles/婚纱/" + category + "/" + name;
            string thumbnailImage = "~/UpFiles/婚纱/" + category + "/Thumbnail/" + name;
            var pictorialInfo = new FileInfo(Server.MapPath(xPath));
            if (pictorialInfo.Exists)
            {
                pictorialInfo.Delete();
            }
            pictorialInfo = new FileInfo(Server.MapPath(thumbnailImage));
            if (pictorialInfo.Exists)
            {
                pictorialInfo.Delete();
            }

            return RedirectToAction("ShowWeddingCategory", new {id = categoryId, name = category});
        }


        [Authorize]
        public ActionResult SetAsFrontCover(string name, int categoryId)
        {
            WeddingCategory category = _service.GetById(categoryId);
            category.ThumbnailPath = "/UpFiles/婚纱/" + category.Name + "/Thumbnail/" + name;
            _service.Update(category);

            return RedirectToAction("WeddingCategoryList");
        }


        [ChildActionOnly]
        //[OutputCache(Duration = 3600)]
        public PartialViewResult _categoryAdmin(string photoType)
        {
            ViewBag.PhotoType = photoType;


            return PartialView("_categoryAdmin", _service.GetAll());
        }
    }
}
